//编辑地址
var addressset = {
    page: 1, //初始页
    pagecount: 0, //总页数
    pagesize: 20, //每页数量
    load: function () {
        addressset.havearea(d.areacode, cid);
    },
    //设定二级区域的高度
    initTop: function () {
        var marTop = $('.cur').position().top + 29;
        $('.son_area_list').css({ "top": marTop });
        //console.log(marTop);
    },
    giveFocus: function (areacode, comid) {
        addressset.showCommuntiy(areacode, comid, "", false, true);
        event.stopPropagation();
    },
    //获取小区列表
    showCommuntiy: function (areaId, companyId, name, isseach, isclick) {
        $('#s_area').val("");
        var s = document.getElementById("s_area");
        if (!s) return;
        $('#community .areaitem').html("");
        $.ajax({
            type: 'POST',
            url: '/ajax/District.ashx',
            dataType: 'json',
            data: { "act": "getcommunity", "areaId": areaId, "cid": companyId, "page": addressset.page, "pagesize": addressset.pagesize, "name": name },
            async: false,
            success: function (msg, textStatus) {
                if (msg.list.length > 0) {
                    if (!isclick) {
                        addressset.pagecount = msg.pagecount;
                        $('#community').show();
                        return;
                    }
                    var __str = '';
                    __str += '<div class="s_area_div" id="s_area_div">';
                    __str += '<ul class="father_area_list">';
                    __str += '<div class = "area_search"><input type = "text" id="search_name" class="area_search_input" placeholder = "找不到所在小区吗 试试搜索吧"/><div class="search_btn" onclick="addressset.search()">搜索</div></div>';
                    __str += '<li onclick="addressset.notinarea()"><i>不在上述小区</i></li>';
                    for (var i = 0; i < msg.list.length; i++) {
                        var name = msg.list[i].C_CommunityName;
                        if (msg.list[i].C_CommunityName.length > 10) //字符串过长时截取
                            __str += '<li onclick="addressset.showbuilding(this)" ><i class="itemarea" title="' + msg.list[i].C_CommunityName + '" communityid=' + msg.list[i].C_CommunityID + '>' + msg.list[i].C_CommunityName.substring(0, 9) + '...' + '</i>';
                        else
                            __str += '<li onclick="addressset.showbuilding(this)" ><i class="itemarea" communityid=' + msg.list[i].C_CommunityID + '>' + name + '</i>';
                        __str += '<ul class="son_area_list">';
                        var buildings = msg.list[i].C_Buildings.split(/[,，]/);
                        for (var l = 0; l < buildings.length; l++) {
                            if (buildings[l]) {
                                var __buliding = buildings[l];
                                if (__buliding.length > 14) {
                                    __buliding = __buliding.substring(0, 14) + '...';
                                }
                                __str += '<li onclick="addressset.getarea(this)" >' + __buliding + '</li>';
                            }
                        }
                        __str += '</ul>';
                        __str += '</li>';
                    }
                    __str += '<div class="clear"></div></ul>';
                    if (msg.pagecount > 1) {
                        __str += '<div class="pages" ><dl><dd class="pre" onclick="addressset.changepage(1)">上一页</dd><a>' + addressset.page + '/' + addressset.pagecount + '</a><dd class="next" onclick="addressset.changepage(0)">下一页</dd><div class="clear"></div></dl></div>';
                    }
                    __str += '</div>';
                    $("#community .areaitem").html(__str);
                    //alert(__str);
                }
                else {
                    $('#community .areaitem').html("");
                    $('#s_area').parent().hide();
                }
                if (msg.list.length < 1 && isseach) {
                    var _str = '';
                    _str += '<div class="s_area_div" id="s_area_div">';
                    _str += '<ul class="father_area_list">';
                    _str += '<div class = "area_search"><input type = "text" id="search_name" class="area_search_input" placeholder = "请输入小区名称"/><div class="search_btn" onclick="addressset.search()">搜索</div></div>';
                    _str += '<li onclick="addressset.notinarea()"><i>不在上述小区</i></li>';
                    _str += '<div class="clear"></div></ul>';
                    _str += '</div>';
                    $("#community .areaitem").html(_str);
                }
            },
            error: function (textStatus) { }
        });
    },
    //显示一级区域
    showbuilding: function (obj) {
        var $this = $(obj);
        if ($this.find('li').length > 0) {
            $this.addClass('cur').siblings('li').removeClass('cur');
            addressset.initTop();
            var $son_area = $this.find('.son_area_list');
            $('.son_area_list').hide(); //隐藏其他
            $son_area.show();
        } else {
            $('#s_area').val($this.text());
            $("#communityid").val($this.find("i").attr('communityid'));
            $this.removeClass('cur');
            $('#s_area_div,.son_area_list').hide();
        }
    },

    //取得二级区域值
    getarea: function (obj) {
        event.stopPropagation();
        var $this = $(obj);
        var father_area_val = $this.parent().parent().find('i').attr('communityid'); //获取一级区域ID
        $this.parent().parent().removeClass('cur');
        var area_val = $this.text(); //获取二级区域
        $("#communityid").val(father_area_val);
        $("#hbuilding").val(area_val);
        $('#s_area').val($this.parent().parent().find('i').text() + ' ' + area_val);
        $('#s_area_div,.son_area_list').hide();

    },
    //不在上述小区
    notinarea: function () {
        $('#s_area_div,.son_area_list').hide();
        $('#s_area').val('');
        $("#communityid").val('');
        $("#hbuilding").val('');
        $('#community').hide();
    },
    //上一页&下一页
    changepage: function (obj) {
        if (obj) {
            if (addressset.page > 1)
                addressset.page = addressset.page - 1;
            else
                addressset.page = 1;
        }
        else {
            if (addressset.page < addressset.pagecount)
                addressset.page = addressset.page + 1;
            else
                addressset.page = addressset.pagecount;
        }
        addressset.showCommuntiy(mjquery.getElem('#street').value, d.cid, "", false, true);
    },
    //商家是否设置小区
    havearea: function (areacode, comid) {
        $.ajax({
            type: 'POST',
            url: '/ajax/District.ashx',
            dataType: 'json',
            data: { "act": "havearea", "areaId": areacode, "cid": comid },
            async: false,
            success: function (msg) {
                if (msg > 0) {
                    $('#community').show();
                }
                else {
                    $('#community').hide();
                }
            }
        });
    },

    search: function () {
        var areacode = mjquery.getElem('#street').value;
        var name = $("#search_name").val();
        addressset.showCommuntiy(areacode, cid, name, true, true);
    }
}
